package com.icid.store.command
{
	import assets.EmbededAssets;
	
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.view.ViewLocator;
	import com.icid.store.business.delegate.UserDelegate;
	import com.icid.store.event.TryLoginEvent;
	import com.icid.store.model.UserModelLocator;
	import com.icid.store.view.components.admin.LoginPanelHelper;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;

	public class TryLoginCommand extends CommandBase implements ICommand, IResponder
	{
		private var delegate:UserDelegate;
		private var tryLoginEvent:TryLoginEvent;
		private var userModel:UserModelLocator = UserModelLocator.getInstance();
		public function TryLoginCommand()
		{
			super();
			delegate = new UserDelegate(this);
		}
		
		override public function execute(event:CairngormEvent):void
		{
			tryLoginEvent = TryLoginEvent(event);
			delegate.read(tryLoginEvent.simpleUser);
		}
		
		override public function result(data:Object):void
		{
			switch(data.result.info)
			{
				case "failure":
					userModel.status = false;
					
					Alert.show("Login Failure!","Failure",4,null,null,EmbededAssets.ALERT_INFO);
					break;
				case "success":
					userModel.status = true;
					userModel.userInfo = tryLoginEvent.simpleUser;
					
					try
					{
						LoginPanelHelper(ViewLocator.getInstance().getViewHelper("loginPanelHelper")).closeWindow();
					}catch(error:Error)
					{
						//Empty;
					}
					break;
				default:
					break;		
			}
		}
		
	}
}